package com.zq.widget.inverted.game.role.impl.plane;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.support.annotation.NonNull;

import com.zq.widget.inverted.game.role.Role;
import com.zq.widget.inverted.game.role.damage.Damage;
import com.zq.widget.inverted.game.role.damage.DamageProxy;
import com.zq.widget.inverted.game.role.decorate.Decorator;
import com.zq.widget.inverted.game.role.decorate.HPBar;
import com.zq.widget.inverted.game.role.equipment.Equipment;
import com.zq.widget.inverted.game.role.equipment.EquipmentRole;
import com.zq.widget.inverted.game.role.equipment.weapon.Weapon;
import com.zq.widget.inverted.game.role.equipment.weapon.gun.Gun2;
import com.zq.widget.inverted.game.role.hp.HP;
import com.zq.widget.inverted.game.role.hp.HPProxy;
import com.zq.widget.inverted.game.role.impl.prop.WeaponUpLevel;
import com.zq.widget.inverted.game.role.impl.type.Hero;
import com.zq.widget.inverted.game.role.impl.type.Prop;
import com.zq.widget.inverted.game.role.level.Level;

/**
 * Created by zhangqiang on 2018/3/28.
 */
public class HeroPlane extends EquipmentRole implements HP,Damage,Hero {

    private HP hpController;
    private Decorator hpBar;
    private Damage damage;

    public HeroPlane(Context context, int drawableResId, HP hpController) {
        super(context, drawableResId);
        this.hpController = hpController;
        hpBar = new HPBar(2, Color.RED, 10,2, hpController);
        damage = new DamageProxy(10);
    }

    public HeroPlane(Context context, int drawableResId, long hp) {
        this(context, drawableResId,new HPProxy(hp));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        hpBar.onDraw(canvas,bounds());
    }

    @Override
    public void onCollisionWith(@NonNull Role target) {
        super.onCollisionWith(target);
        if(target instanceof Prop){
            Object take = ((Prop) target).take();
            if(take instanceof Weapon){
                addEquipment((Weapon) take);
            }
            if(take instanceof WeaponUpLevel){

                WeaponUpLevel weaponUpLevel = (WeaponUpLevel) take;
                Equipment equipment = findEquipment(weaponUpLevel.getType());
                if(equipment instanceof Level){
                    ((Level) equipment).updateLevel(weaponUpLevel.getLevel());
                }
            }
        }
    }

    @Override
    public boolean isFinish() {
        return super.isFinish() || getHp() <= 0;
    }

    @Override
    public long totalHp() {
        return hpController.totalHp();
    }

    @Override
    public long getHp() {
        return hpController.getHp();
    }

    @Override
    public boolean addHp(long hp) {
        return hpController.addHp(hp);
    }

    @Override
    public boolean reduceHp(long hp) {
        return hpController.reduceHp(hp);
    }

    @Override
    public long damage() {
        return damage.damage();
    }

    @Override
    protected boolean addChildrenToParentWhenFinish() {
        return false;
    }
}
